#include "Tile.h"

#include <cassert>

#include "../SGD Wrappers/CSGD_TextureManager.h"

#include "EventSystem.h"
#include "Player.h"

CTile::CTile(void)
{
	this->m_bCollidable = false;
	this->m_bHasEvent = false;
	this->m_strTrigger = "";
	this->SetType(OBJ_TILE);
}

CTile::~CTile(void)
{

}

bool CTile::CheckCollision(IEntity* pOther)
{
	//If the object that made contact was a player
	if(CEntity::CheckCollision(pOther) && pOther->GetType() == OBJ_PLAYER)
	{
		CPlayer* pPlayer = dynamic_cast<CPlayer*>(pOther);
		
		if(this->m_bHasEvent)
		{
			this->m_bHasEvent = false;
			this->m_bCollidable = false;

			//Sends the event for the player to handle
			CEventSystem::GetInstance()->SendEvent(this->m_strTrigger, pPlayer);

			//Triggers shouldn't stop the player from moving, so return now
			return true;
		}

		//Relocate the player, so they are no longer colliding with said tile
		pPlayer->SetVelX(-pPlayer->GetVelX());
		pPlayer->SetVelY(-pPlayer->GetVelY());

		//////////////////////////////
		//Pseudo possible code ///////
		/*This would allow an object to anticipate
		collision before actually moving
		
		ObjectManager's CheckCollision ??(float time)??		//This gets called inside GameplayState's Update, so time is available)
		for(each object)
		{
		 float1 = X
		 float2 = Y
		 X += VelX * time
		 Y += VelY * time

		 for(each other object)
		 {
		  if(Collision against another object)	//Returns true if the 2 objects would be colliding
		   VelX = VelY = 0	//this would stop the object from moving on top of the other object
		 }

		 X = float1
		 Y = float2
		}
		ObjectManager's Update
		*/

		return true;
	}

	return false;
}